#include "encode.h"



//编码器处理函数
void encoder_init()
{
    device_gpio_key_init(GPIO_ENCODE_B, GPIO_MODE_INPUT);
    device_gpio_key_init(GPIO_ENCODE_A, GPIO_MODE_INPUT);
}

EM_ENCODER_ACT encoder_scan()
{
	static u8 last_encode_b;
	static u8 encode_first_flg = 0;
    static u32 triggerTim=0; 
    static int ab_flag = 0;
    static int subtime;
    static u8 kit = 0;
	u8 cur_b = 0;
	u8 cur_a = 0;
    EM_ENCODER_ACT act = ENCODER_ACT_NONE;
    u8 flg_h = 0;

    if(arch_os_ms_elapsed(subtime) >= (500))
    {
        ab_flag = 0;
        kit = 0;
    }

	if(!encode_first_flg)   //?
	{
		last_encode_b = GET_ENCODER_B();

		encode_first_flg = 1;
		//LOG_INFO("zhl add -> first enter encoder_scan2,refesh it");
		return ENCODER_ACT_NONE;
	}

	cur_b = GET_ENCODER_B();
	cur_a = GET_ENCODER_A();

    triggerTim += ENCODE_SCAN_PERIOD;

    if(GET_PRESS == LEV_KEY_OPT_PRESSED) //按下去了
    {
        if(last_encode_b != cur_b)
        {
            if(cur_b != cur_a)
            {   
                if (ab_flag == 1)
                {
                    if (kit < 2)
                    {
                        kit++;
                    }
                    else
                    {
                        kit = 0;
                        ab_flag = 2;
                    }
                
                }
                else 
                {
                    kit = 0;
                    act = ENCODER_PRE_TR;
                    subtime=arch_os_ms_now();
                    ab_flag = 2;
                    LOG_INFO("ENCODER_PRE_TR\n");
                    LOG_INFO("cur_b %d,cur_a %d\n",cur_b,cur_a);
                    clear_key_update();
                }
            }
            else
            {
                if (ab_flag == 2)
                {
                    if(kit < 2)
                    {
                        kit++;
                    }
                    else
                    {
                        kit=0;
                        ab_flag = 1;
                    }
                
                }
                else
                {
                    kit = 0;
                    act = ENCODER_PRE_TL;
                    subtime=arch_os_ms_now();
                    ab_flag = 1;
                    LOG_INFO("ENCODER_PRE_TL\n");
                    LOG_INFO("cur_b %d,cur_a %d\n",cur_b,cur_a);
                    clear_key_update();
                }
            }
            triggerTim = 0;
		    last_encode_b = cur_b;
        }
    }
    else if(GET_PRESS == LEV_KEY_OPT_UNPRESS) //没有按下去
    {
        if(last_encode_b != cur_b)
        {
            if(cur_b != cur_a)
            {   
                if (ab_flag == 1)
                {
                    if (kit < 2)
                    {
                        kit++;
                    }
                    else
                    {
                        kit = 0;
                        ab_flag = 2;
                    }
                
                }
                else 
                {
                    kit = 0;
                    act = ENCODER_ACT_TR_L;
                    subtime=arch_os_ms_now();
                    ab_flag = 2;
                    LOG_INFO("ENCODER_ACT_TR_L\n");
                    LOG_INFO("cur_b %d,cur_a %d\n",cur_b,cur_a);
                    clear_key_update();
                }
            }
            else
            {
                if (ab_flag == 2)
                {
                    if(kit < 2)
                    {
                        kit++;
                    }
                    else
                    {
                        kit=0;
                        ab_flag = 1;
                    }
                
                }
                else
                {
                    kit = 0;
                    act = ENCODER_ACT_TL_L;
                    subtime=arch_os_ms_now();
                    ab_flag = 1;
                    LOG_INFO("ENCODER_ACT_TL_L\n");
                    LOG_INFO("cur_b %d,cur_a %d\n",cur_b,cur_a);
                    clear_key_update();
                }
            }
            triggerTim = 0;
		    last_encode_b = cur_b;
        }
        
    }
    return act;
}

